#include "NetworkMessage.h"
using namespace std;
const unsigned int NetworkMessage::TYPE_CHAT_MESSAGE = 0;
const unsigned int NetworkMessage::TYPE_ECHO_MESSAGE = 1;
const unsigned int NetworkMessage::TYPE_ECHO_REPLY_MESSAGE = 2;
NetworkMessage::NetworkMessage( void ) {}

NetworkMessage::NetworkMessage( NetworkByteArray data )
{
	cout<<(int)data[0]<<endl;
	// The fisrt unsigned char is the type of the message
	m_type = data.getUnsignedChar( 0 );
	switch( m_type )
	{
		case TYPE_CHAT_MESSAGE:
				m_chat = new ChatMessage( data );
				break;
				
		default:
			cout << "Type non supporté : " << ( int ) data.getUnsignedChar( 0 ) << endl;
			break;
	}
}


NetworkMessage::~NetworkMessage( void ) 
{
	switch( m_type)
	{
		case TYPE_CHAT_MESSAGE:
			delete m_chat;
			break;
		case TYPE_ECHO_MESSAGE:
		case TYPE_ECHO_REPLY_MESSAGE:
			break;
	}
}

unsigned char NetworkMessage::getType( void )
{
	return m_type;
}


NetworkByteArray NetworkMessage::serialize( void )
{
	NetworkByteArray returnArray;

	switch( m_type )
	{
		case TYPE_CHAT_MESSAGE:
			returnArray = m_chat->serialize();
			returnArray.insertAt( m_type, 0);
			break;
		case TYPE_ECHO_MESSAGE:
		case TYPE_ECHO_REPLY_MESSAGE:
			returnArray.push( m_type );
			break;
	}
	return returnArray;
}

void NetworkMessage::setType( unsigned char type )
{
	m_type = type;
}
